package com.greenantmall.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.view.View;
import android.widget.LinearLayout;

import com.avos.avoscloud.AVObject;
import com.greenantmall.R;
import com.greenantmall.utils.AlipayUtils;
import com.greenantmall.utils.PayCompleteListener;
import com.greenantmall.utils.UtilTools;

import net.sourceforge.simcpux.WeChatPayUtil;

import java.util.ArrayList;
import java.util.List;

public class ChoosePayMethodDialog extends Dialog implements
		android.view.View.OnClickListener, OnDismissListener {
	private Activity mActivity;
	private LinearLayout mBtnChooseAlipay;
	private LinearLayout mBtnChooseWechatpay;
	private List<AVObject> mCreatedOrdersList = new ArrayList<AVObject>();
	private PayCompleteListener mPayCompletListener;
	private boolean isChoosedPayMethod = false;

	public ChoosePayMethodDialog(Activity activity, List<AVObject> createdOrdersList,
			PayCompleteListener listener) {
		super(activity, R.style.choose_date_dialog_style);
		setContentView(R.layout.choose_pay_method_dialog);
		mActivity = activity;
		this.mCreatedOrdersList.addAll(createdOrdersList);
		this.mPayCompletListener = listener;
		mBtnChooseAlipay = (LinearLayout) findViewById(R.id.choose_pay_method_alipay);
		mBtnChooseWechatpay = (LinearLayout) findViewById(R.id.choose_pay_method_wechatpay);
		mBtnChooseAlipay.setOnClickListener(this);
		mBtnChooseWechatpay.setOnClickListener(this);
		this.setOnDismissListener(this);
	}

	@Override
	public void onClick(View v) {
		if (v.equals(mBtnChooseAlipay)) {
			// 支付宝支付
			isChoosedPayMethod = true;
			AlipayUtils payUtils = new AlipayUtils(
					mActivity,
					mCreatedOrdersList, mPayCompletListener);
			payUtils.alipay();
			this.dismiss();
		} else if (v.equals(mBtnChooseWechatpay)) {
			isChoosedPayMethod = true;
			WeChatPayUtil weChatPayUtil=WeChatPayUtil.getInstance();
			weChatPayUtil.setData(
                    mActivity,
                    mCreatedOrdersList, mPayCompletListener);
			weChatPayUtil.wechatPay();
			// 微信支付
//			UtilTools.showMyToast(mActivity, mActivity.getResources().getString(R.string.pay_method_wechatpay));
			this.dismiss();
		}
	}

	@Override
	public void onDismiss(DialogInterface dialog) {
		if(!isChoosedPayMethod && mPayCompletListener != null) {
			mPayCompletListener.notChoosePayMethod();
		}
	}
}
